/**
 * %%
 * %%
 */
package org.apache.flink.runtime.allocation;

import org.apache.flink.configuration.MemorySize;
import org.apache.flink.runtime.resourcemanager.ResourceOverview;

/**
 * @类名: ResourceAllocationStatics
 * @说明: 资源分配统计
 *        1 资源管理器视图，即，全局资源统计
 *        2 资源消费者视图
 *
 * @author   leehom
 * @Date	 2024年1月23日 下午6:02:51
 * 修改记录：
 *
 * @see 	 
 */
public class ResourceAllocationStatistics {

	/** 可用资源slot数*/
	private int numSlotsAvailable;
	/** 已分配slot数*/
	private int numSlotAllocated;
	/** 资源slot总数*/
	private int numSlotTotal;
	/** cpu core数量*/
	private int numCpuCoreTotal;
	/** 堆内存总数量*/
	private MemorySize numHeapMemoryTotal;
    /** 资源提供者数量*/
    private int numResourcePoviders;
    // TBD
    // ResourceProfile 资源slot资源profile
    
	public ResourceAllocationStatistics(int numSlotsAvailable, int numSlotAllocated, int numSlotTotal,
			int numCpuCoreTotal, MemorySize numHeapMemoryTotal, int numResourceProvider) {
		super();
		this.numSlotsAvailable = numSlotsAvailable;
		this.numSlotAllocated = numSlotAllocated;
		this.numSlotTotal = numSlotTotal;
		this.numCpuCoreTotal = numCpuCoreTotal;
		this.numHeapMemoryTotal = numHeapMemoryTotal;
		this.numResourcePoviders = numResourceProvider;
	}
	
	public int getNumSlotTotal() {
		return numSlotTotal;
	}
	public int getNumSlotsAvailable() {
		return numSlotsAvailable;
	}
	public int getNumSlotAllocated() {
		return numSlotAllocated;
	}
	public int getNumCpuCoreTotal() {
		return numCpuCoreTotal;
	}
	public MemorySize getNumHeapMemoryTotal() {
		return numHeapMemoryTotal;
	}
	public int getNumWorks() {
		return numResourcePoviders;
	}
	
	//
	public static ResourceAllocationStatistics from(ResourceOverview resourceOverview) {
		
	    int numResourcePoviders = resourceOverview.getNumberWorkers();
		//
		int numSlotTotal = resourceOverview.getNumberRegisteredSlots();
		// 
		int numSlotAllocated = resourceOverview.getNumberFreeSlots();
		//
		int numSlotsAvailable = numSlotTotal - numSlotAllocated;
				
		int numCpuCoreTotal = resourceOverview.getTotalResource().getCpuCores().getValue().intValue();
		/** 堆内存总数量*/
		MemorySize numHeapMemoryTotal = resourceOverview.getTotalResource().getTaskHeapMemory();
	    /** 资源提供者数量*/

		return new ResourceAllocationStatistics(numSlotsAvailable, numSlotAllocated,
				numSlotTotal, numCpuCoreTotal, numHeapMemoryTotal, numResourcePoviders);
		
	}
	
}
